import http from './axios'; // Adjust the import path as necessary

// 获取所有测试
export const getAllTests = () => {
  return http.request({
    url: '/api/tests',
    method: 'get'
  }).catch(error => {
    console.error('Error fetching all tests:', error);
    throw error;
  });
};

// 根据ID获取测试
export const getTestById = (id) => {
  return http.request({
    url: `/api/tests/${id}`,
    method: 'get'
  }).catch(error => {
    console.error(`Error fetching test with id ${id}:`, error);
    throw error;
  });
};

// 创建新的测试
export const createTest = (data) => {
  return http.request({
    url: '/api/tests',
    method: 'post',
    data
  }).catch(error => {
    console.error('Error creating test:', error);
    throw error;
  });
};

// 更新测试
// api/TestService.js

export const updateTest = async (id, test) => {
  if (!id) {
    throw new Error('ID 是更新测试所必需的');
  }
  try {
    const response = await http.request({
      method: 'put',
      url: `/api/tests/${id}`,
      data: test
    });
    return response.data;
  } catch (error) {
    console.error('更新测试时出错，ID:', id, '错误:', error);
    throw error;
  }
};

// 删除测试
export const deleteTest = (id) => {
  return http.request({
    url: `/api/tests/${id}`,
    method: 'delete'
  }).catch(error => {
    console.error(`Error deleting test with id ${id}:`, error);
    throw error;
  });
};

// 搜索测试
export const searchTests = (params) => {
  return http.request({
    url: '/api/tests/search',
    method: 'get',
    params: { keyword: params.keyword } // 确保参数格式正确
  }).catch(error => {
    console.error('Error searching tests:', error);
    throw error;
  });
};
// 按类型获取测试
// 按类型获取测试
export const getTestsByTypes = (types) => {
  return http.request({
    url: '/api/tests/types',
    method: 'get',
    params: { types: types.join(',') } // 将数组转换为逗号分隔的字符串
  }).catch(error => {
    console.error('Error fetching tests by types:', error);
    throw error;
  });
};

// 获取单个题目的状态
export const getTestStatus = (id) => {
  return http.request({
    url: `/api/tests/${id}/status`,
    method: 'get'
  }).catch(error => {
    console.error(`Error fetching test status with id ${id}:`, error);
    throw error;
  });
};

// 获取多个题目的状态
export const getTestsStatus = (ids) => {
  return http.request({
    url: '/api/tests/status',
    method: 'get',
    params: { ids: ids.join(',') } // 将数组转换为逗号分隔的字符串
  }).catch(error => {
    console.error('Error fetching tests status:', error);
    throw error;
  });
};